PAY = {
	urls:{
		admin 	: "/pay/admin",
		account : "/pay/account",
		scan 	: "/pay/scan",
		query 	: "/pay/query",
        union 	: "/pay/unionpay",
	},
	init:function(){
		var order_id = 0;
		var urls = PAY.urls;
		//pay admin
		POST.init(document,{
			method:'bc-pay-admin',
			stop:function(){
				PAY.channel = 'adminpay';
				PAY.paying.order_id = 0;
				order_id = PAY.order_id;
				if(!order_id){
					return true;
				}
			},
			url:urls.admin,
			input:function(){
				return {order_id:order_id};
			},
			text:'现金支付',
			success:function(){
				$(document).trigger('bc-pay-success',PAY.order_id)
				$(document).trigger('bc-pay-admin-success',PAY.order_id)
				PAY.order_id = 0;
			}
		})
        POST.init(document,{
            method:'bc-pay-union',
            stop:function(){
                PAY.channel = 'unionpay';
                PAY.paying.order_id = 0;
                order_id = PAY.order_id;
                if(!order_id){
                    return true;
                }
            },
            url:urls.union,
            input:function(){
                return {order_id:order_id};
            },
            text:'银联支付',
            success:function(){
                $(document).trigger('bc-pay-success',PAY.order_id)
                $(document).trigger('bc-pay-union-success',PAY.order_id)
                PAY.order_id = 0;
            }
        })
		//pay account
		POST.init(document,{
			method:'bc-pay-account',
			stop:function(){
				PAY.channel = 'accountpay';
				PAY.paying.order_id = 0;
				order_id = PAY.order_id;
				if(!order_id){
					return true;
				}
			},
			url:urls.account,
			input:function(){
				var auth_code = PAY.auth_code;
				return {order_id:order_id,auth_code:auth_code};
			},
			text:'会员账户支付',
			success:function(){
				$(document).trigger('bc-pay-success',PAY.order_id)
				$(document).trigger('bc-pay-admin-success',PAY.order_id)
				PAY.order_id = 0;
				PAY.auth_code = 0;
			}
		})
		var auth_code,body
		//pay scan
		POST.init(document,{
			method:'bc-pay-scan',
			stop:function(){
				// var auth_code = $().val();
				PAY.paying.order_id = 0;
				order_id 	= PAY.order_id;
				auth_code 	= PAY.auth_code;
				body 		= PAY.body;
				if(auth_code.substr(0,2) == '13') PAY.channel = 'tenpay';
				else if(auth_code.substr(0,2) == '28') PAY.channel = 'alipay';
				if(!auth_code) return true;
				if(!body) return true
				if(!order_id){
					return true;
				}
			},
			url:urls.scan,
			input:function(){
				order_id 	= PAY.order_id;
				auth_code 	= PAY.auth_code;
				body 		= PAY.body;
				return {order_id:order_id,auth_code:auth_code,body:body};
			},
			text:'线上支付',
			success:function(){
				$(document).trigger('bc-pay-success',PAY.order_id)
				$(document).trigger('bc-pay-admin-success',PAY.order_id)
				PAY.order_id = 0;
				PAY.auth_code 	= '' ;
				PAY.body 		= '' ;
			},
			error:function(e,res){
				switch(res.errorcode){
					case 'USERPAYING':
					PAY.order_id = 0;
					MODAL.close('.modal');
					TOAST.stop();
					TOAST.load("用户正在输入密码",true,15)
					console.log('pay paying start');
					console.log('pay paying start');
					console.log('pay paying start');
					console.log('pay paying start');
					PAY.paying.start(order_id);
					break;
				}
			},
			msg:{
				'USERPAYING':'用户正在输入密码',
				'auth_code error':'支付码错误'
			}
		})
	},
	paying:{
		order_id : 0,
		start:function(order_id){
			console.log(this.order_id,order_id)
			if(this.order_id){
				this.order_id = order_id;
				return;
			}
			this.order_id = order_id;
			this.loading();
		},
		loading:function(){
			var self = this;
			if(!self.order_id) return;
			$.ajax({
				url: PAY.urls.query,
				data: {order_id: self.order_id},
				success:function(res){
					console.log('PAY QUERY',res,self.order_id);
					if(res.state){
						$(document).trigger('bc-pay-success',self.order_id)
						$(document).trigger('bc-pay-query-success',self.order_id)
						self.order_id = 0;
						TOAST.success('支付成功',1,3);
						return
					}
				}
			})
			setTimeout(function(){
				self.loading();
			},1000)
		}
	}
}
$(function(){
	PAY.init();	
})